package com.zjb.datastructure.tree;

public class Trie {

    class Node{
        private char data;
        private Node[] children=new Node[26];
        private Boolean isEndChar = false;

        public Node(char data) {
            this.data = data;
        }
    }

    private final Node root = new Node('/');


    private void insert(String str) {
        char[] chars = str.toCharArray();
        Node parent = root;
        for (char aChar : chars) {
            int index = ((int) aChar) - ((int) 'a');
            if (parent.children[index] == null) {
                parent.children[index] = new Node(aChar);
            }
            parent = parent.children[index];
        }
        parent.isEndChar = true;
    }

    private boolean find(String str) {
        char[] chars = str.toCharArray();
        Node parent = root;
        for (char aChar : chars) {
            int index = ((int) aChar) - ((int) 'a');
            if (parent.children[index] == null) {
                return false;
            }
            parent = parent.children[index];

        }
        return parent.isEndChar;

    }


    public static void main(String[] args) {
        Trie trie = new Trie();
        trie.insert("abc");
        trie.insert("abd");
        trie.insert("bc");


        System.out.println(trie.find("abc"));
        System.out.println(trie.find("bca"));
    }




}
